{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 字符串相关操作",
   "id": "8bfed49d98df6604"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T10:47:25.981Z",
     "start_time": "2025-09-16T10:47:24.889093Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "from PIL.ImageOps import expand\n",
    "\n",
    "path = 'D:/2506A/monty03/day17/file/'"
   ],
   "id": "c0cb9d2ec0569c52",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T10:57:12.904166Z",
     "start_time": "2025-09-16T10:57:12.868423Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "\n",
    "print(df['姓名'].str.cat()) # 将所有的名字拿出来拼接成一个字符串\n",
    "print(df['姓名'].str.cat(sep=',')) # 将所有的名字拿出来拼接成一个字符串 用逗号分割\n",
    "print(df['姓名'].str.cat(['很美'] * len(df)))\n",
    "print(df['姓名'].str.cat(['很美'] * len(df),sep=','))\n",
    "print(df['姓名'].str.cat(['很美'] * len(df),sep=',',na_rep='@')) # na_rep 用于替换NAN"
   ],
   "id": "8ee21ad517afdbb9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "谭鑫宇聂茹凤韩耀祖刘千琪李欣桐李兆康\n",
      "谭鑫宇,聂茹凤,韩耀祖,刘千琪,李欣桐,李兆康\n",
      "0    谭鑫宇很美\n",
      "1    聂茹凤很美\n",
      "2    韩耀祖很美\n",
      "3    刘千琪很美\n",
      "4    李欣桐很美\n",
      "5      NaN\n",
      "6    李兆康很美\n",
      "Name: 姓名, dtype: object\n",
      "0    谭鑫宇,很美\n",
      "1    聂茹凤,很美\n",
      "2    韩耀祖,很美\n",
      "3    刘千琪,很美\n",
      "4    李欣桐,很美\n",
      "5       NaN\n",
      "6    李兆康,很美\n",
      "Name: 姓名, dtype: object\n",
      "0    谭鑫宇,很美\n",
      "1    聂茹凤,很美\n",
      "2    韩耀祖,很美\n",
      "3    刘千琪,很美\n",
      "4    李欣桐,很美\n",
      "5      @,很美\n",
      "6    李兆康,很美\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## split按照指定字符串分割",
   "id": "e4364c26ff16b994"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:03:36.050183Z",
     "start_time": "2025-09-16T11:03:36.020990Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['状态'].str.split('血'))\n",
    "print(df['状态'].str.split()) # 如果不指定分隔符 返回一个列表\n",
    "print(df['状态'].str.split('血',n=-1)) # n=-1表示全部分割\n",
    "print(df['状态'].str.split('血',expand=True))"
   ],
   "id": "2afeb728ec08ac12",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    [满, 激, 活]\n",
      "1      [零, 销毁]\n",
      "2      [满, 激活]\n",
      "3      [满, 激活]\n",
      "4      [零, 销毁]\n",
      "5      [零, 销毁]\n",
      "6      [零, 销毁]\n",
      "Name: 状态, dtype: object\n",
      "0    [满血激血活]\n",
      "1     [零血销毁]\n",
      "2     [满血激活]\n",
      "3     [满血激活]\n",
      "4     [零血销毁]\n",
      "5     [零血销毁]\n",
      "6     [零血销毁]\n",
      "Name: 状态, dtype: object\n",
      "0    [满, 激, 活]\n",
      "1      [零, 销毁]\n",
      "2      [满, 激活]\n",
      "3      [满, 激活]\n",
      "4      [零, 销毁]\n",
      "5      [零, 销毁]\n",
      "6      [零, 销毁]\n",
      "Name: 状态, dtype: object\n",
      "   0   1     2\n",
      "0  满   激     活\n",
      "1  零  销毁  None\n",
      "2  满  激活  None\n",
      "3  满  激活  None\n",
      "4  零  销毁  None\n",
      "5  零  销毁  None\n",
      "6  零  销毁  None\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## partition按照指定字符分割",
   "id": "31a9df2449651962"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:08:34.670935Z",
     "start_time": "2025-09-16T11:08:34.647295Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['状态'].str.partition())\n",
    "print(df['状态'].str.partition('血'))\n",
    "print('BbBbB'.partition('b'))  # B  b  BbB\n",
    "print(df['状态'].str.partition('水'))"
   ],
   "id": "298e2ecef5c83287",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0 1 2\n",
      "0  满血激血活    \n",
      "1   零血销毁    \n",
      "2   满血激活    \n",
      "3   满血激活    \n",
      "4   零血销毁    \n",
      "5   零血销毁    \n",
      "6   零血销毁    \n",
      "   0  1    2\n",
      "0  满  血  激血活\n",
      "1  零  血   销毁\n",
      "2  满  血   激活\n",
      "3  满  血   激活\n",
      "4  零  血   销毁\n",
      "5  零  血   销毁\n",
      "6  零  血   销毁\n",
      "('B', 'b', 'BbB')\n",
      "       0 1 2\n",
      "0  满血激血活    \n",
      "1   零血销毁    \n",
      "2   满血激活    \n",
      "3   满血激活    \n",
      "4   零血销毁    \n",
      "5   零血销毁    \n",
      "6   零血销毁    \n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## get获取指定位置的字符，只能获取一个",
   "id": "61234bd5f18a33b3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:11:47.375955Z",
     "start_time": "2025-09-16T11:11:47.353810Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['状态'])\n",
    "print(df['状态'].str.get(1))  # 血"
   ],
   "id": "92a622073a93edd7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    满血激血活\n",
      "1     零血销毁\n",
      "2     满血激活\n",
      "3     满血激活\n",
      "4     零血销毁\n",
      "5     零血销毁\n",
      "6     零血销毁\n",
      "Name: 状态, dtype: object\n",
      "0      活\n",
      "1    NaN\n",
      "2    NaN\n",
      "3    NaN\n",
      "4    NaN\n",
      "5    NaN\n",
      "6    NaN\n",
      "Name: 状态, dtype: object\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## slice获取指定范围的字符",
   "id": "f64d2fc3d34b18f4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:17:08.446159Z",
     "start_time": "2025-09-16T11:17:08.420755Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.slice(0)) # 相当于 [0:]\n",
    "print(df['姓名'].str.slice(0,1)) # 相当于[0:1]\n",
    "print(df['状态'].str.slice(0,3,2)) # 相当于[0,3:2]\n",
    "print(df['状态'].str.slice(0,10,2)) # 相当于[0,10:2] # 如果越界，默认到最后"
   ],
   "id": "7cf59713a47ab893",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    谭鑫宇\n",
      "1    聂茹凤\n",
      "2    韩耀祖\n",
      "3    刘千琪\n",
      "4    李欣桐\n",
      "5    NaN\n",
      "6    李兆康\n",
      "Name: 姓名, dtype: object\n",
      "0      谭\n",
      "1      聂\n",
      "2      韩\n",
      "3      刘\n",
      "4      李\n",
      "5    NaN\n",
      "6      李\n",
      "Name: 姓名, dtype: object\n",
      "0    满激\n",
      "1    零销\n",
      "2    满激\n",
      "3    满激\n",
      "4    零销\n",
      "5    零销\n",
      "6    零销\n",
      "Name: 状态, dtype: object\n",
      "0    满激活\n",
      "1     零销\n",
      "2     满激\n",
      "3     满激\n",
      "4     零销\n",
      "5     零销\n",
      "6     零销\n",
      "Name: 状态, dtype: object\n",
      "0     谭宇\n",
      "1     聂凤\n",
      "2     韩祖\n",
      "3     刘琪\n",
      "4     李桐\n",
      "5    NaN\n",
      "6     李康\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## slice_replace筛选出来之后替换",
   "id": "436955ca1d2645fa"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:19:02.629944Z",
     "start_time": "2025-09-16T11:19:02.608295Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['状态'].str.slice_replace(1,3,'520'))"
   ],
   "id": "50b3c01c0c7c8b58",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    满520血活\n",
      "1     零520毁\n",
      "2     满520活\n",
      "3     满520活\n",
      "4     零520毁\n",
      "5     零520毁\n",
      "6     零520毁\n",
      "Name: 状态, dtype: object\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## join将每个字符之间使用指定字符连接",
   "id": "e4f887626078fe8e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:20:11.659812Z",
     "start_time": "2025-09-16T11:20:11.639903Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.join('$'))"
   ],
   "id": "7fc1a0db3304eca4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    谭$鑫$宇\n",
      "1    聂$茹$凤\n",
      "2    韩$耀$祖\n",
      "3    刘$千$琪\n",
      "4    李$欣$桐\n",
      "5      NaN\n",
      "6    李$兆$康\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## contatins判断字符串是否含有指定的子串",
   "id": "e2d5a9ae7df3b1e1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:30:39.480040Z",
     "start_time": "2025-09-16T11:30:39.451329Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.contains('聂'))\n",
    "print(df['姓名'].str.contains('聂',na=False)) # na 遇到NAN的时候，返回什么\n",
    "print(df['姓名'].str.contains('聂',na=True))\n",
    "print(df['姓名'].str.contains('聂',na='空值'))"
   ],
   "id": "53b1ec68507158ac",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5      NaN\n",
      "6    False\n",
      "Name: 姓名, dtype: object\n",
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6    False\n",
      "Name: 姓名, dtype: bool\n",
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5     True\n",
      "6    False\n",
      "Name: 姓名, dtype: bool\n",
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5       空值\n",
      "6    False\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## startswith：判断是否以某个子串开头",
   "id": "fd07d493e6f872a2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:32:19.774974Z",
     "start_time": "2025-09-16T11:32:19.754162Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.startswith('李'))"
   ],
   "id": "8311a1e2bd5f502a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4     True\n",
      "5      NaN\n",
      "6     True\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## endswith:判断是否以某个子串结尾",
   "id": "5c76c6ed893bbc75"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:32:36.908070Z",
     "start_time": "2025-09-16T11:32:36.886672Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.endswith('凤'))"
   ],
   "id": "3f39d9203242b5c7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5      NaN\n",
      "6    False\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## repeat重复字符串",
   "id": "9b952b54365d72c7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:33:24.906049Z",
     "start_time": "2025-09-16T11:33:24.883929Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.repeat(3))"
   ],
   "id": "63be5aa14e436f56",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    谭鑫宇谭鑫宇谭鑫宇\n",
      "1    聂茹凤聂茹凤聂茹凤\n",
      "2    韩耀祖韩耀祖韩耀祖\n",
      "3    刘千琪刘千琪刘千琪\n",
      "4    李欣桐李欣桐李欣桐\n",
      "5          NaN\n",
      "6    李兆康李兆康李兆康\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## pad 将每一个元素都用指定的字符填充，记住只能是一个字符",
   "id": "53efbaed0466b3a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:36:06.632957Z",
     "start_time": "2025-09-16T11:36:06.604688Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.pad(8,fillchar='$'))\n",
    "print(df['姓名'].str.pad(8,fillchar='$',side='right'))\n",
    "print(df['姓名'].str.pad(8,fillchar='$',side='both'))"
   ],
   "id": "d61816b1cb0772e0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    $$$$$谭鑫宇\n",
      "1    $$$$$聂茹凤\n",
      "2    $$$$$韩耀祖\n",
      "3    $$$$$刘千琪\n",
      "4    $$$$$李欣桐\n",
      "5         NaN\n",
      "6    $$$$$李兆康\n",
      "Name: 姓名, dtype: object\n",
      "0    谭鑫宇$$$$$\n",
      "1    聂茹凤$$$$$\n",
      "2    韩耀祖$$$$$\n",
      "3    刘千琪$$$$$\n",
      "4    李欣桐$$$$$\n",
      "5         NaN\n",
      "6    李兆康$$$$$\n",
      "Name: 姓名, dtype: object\n",
      "0    $$谭鑫宇$$$\n",
      "1    $$聂茹凤$$$\n",
      "2    $$韩耀祖$$$\n",
      "3    $$刘千琪$$$\n",
      "4    $$李欣桐$$$\n",
      "5         NaN\n",
      "6    $$李兆康$$$\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 66
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## zfill 填充，只能是0，从左边填充",
   "id": "e53f01f7537c25d4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:37:25.714650Z",
     "start_time": "2025-09-16T11:37:25.691054Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.zfill(5))"
   ],
   "id": "df4e15794c591af2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    00谭鑫宇\n",
      "1    00聂茹凤\n",
      "2    00韩耀祖\n",
      "3    00刘千琪\n",
      "4    00李欣桐\n",
      "5      NaN\n",
      "6    00李兆康\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## encode编码 decode解码",
   "id": "db0a0bbb3c147979"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:39:14.036924Z",
     "start_time": "2025-09-16T11:39:14.012100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['姓名'].str.encode('UTF-8').str.decode('UTF-8'))"
   ],
   "id": "7e67d8152b1875bb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    谭鑫宇\n",
      "1    聂茹凤\n",
      "2    韩耀祖\n",
      "3    刘千琪\n",
      "4    李欣桐\n",
      "5    NaN\n",
      "6    李兆康\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## strip 按照指定内容，从两边去除",
   "id": "e80246a0f4756d96"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:41:57.416580Z",
     "start_time": "2025-09-16T11:41:57.394860Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['里程'].str.strip('中距离'))"
   ],
   "id": "8fe3fe420b39d298",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    近\n",
      "1    远\n",
      "2    远\n",
      "3    近\n",
      "4    远\n",
      "5    远\n",
      "6    近\n",
      "Name: 里程, dtype: object\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## get_dummies 按照指定字符分割，得到列表",
   "id": "26a4842102d87d98"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:48:56.583302Z",
     "start_time": "2025-09-16T11:48:56.550426Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['里程'])\n",
    "print(df['里程'].str.get_dummies('距'))\n",
    "print(df['姓名2'].str.get_dummies('茹')) # 结果中的0和1  1表示该行包含该字符，0表示不包含"
   ],
   "id": "1c8cd357b80da3a8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     近距离\n",
      "1     远距离\n",
      "2    中远距离\n",
      "3     近距离\n",
      "4    中远距离\n",
      "5     远距离\n",
      "6     近距离\n",
      "Name: 里程, dtype: object\n",
      "   中远  离  近  远\n",
      "0   0  1  1  0\n",
      "1   0  1  0  1\n",
      "2   1  1  0  0\n",
      "3   0  1  1  0\n",
      "4   1  1  0  0\n",
      "5   0  1  0  1\n",
      "6   0  1  1  0\n",
      "   信  凤  北  找  李  果  王  聂  谭  陈  非  韩\n",
      "0  0  1  0  0  0  0  0  1  0  0  0  0\n",
      "1  0  0  0  0  1  0  0  0  0  0  0  0\n",
      "2  1  0  0  0  0  0  0  0  1  0  0  0\n",
      "3  0  0  0  0  0  0  0  0  0  0  1  1\n",
      "4  0  0  0  0  0  1  0  0  0  1  0  0\n",
      "5  0  0  0  1  0  0  0  0  0  0  0  0\n",
      "6  0  0  1  0  0  0  1  0  0  0  0  0\n"
     ]
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## translate 指定部分替换",
   "id": "ffa2137cf69eb3b1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T11:56:48.704696Z",
     "start_time": "2025-09-16T11:56:48.691473Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['里程'])\n",
    "\n",
    "#str.translate() 方法需要传入一个翻译表（translation table），这个表通常用 str.maketrans() 创建。\n",
    "dict1 = str.maketrans({\n",
    "    '距':'ju',\n",
    "    '离':'li'\n",
    "})\n",
    "print(df['里程'].str.translate(dict1))"
   ],
   "id": "20d1d47bbc221682",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     近距离\n",
      "1     远距离\n",
      "2    中远距离\n",
      "3     近距离\n",
      "4    中远距离\n",
      "5     远距离\n",
      "6     近距离\n",
      "Name: 里程, dtype: object\n",
      "0     近juli\n",
      "1     远juli\n",
      "2    中远juli\n",
      "3     近juli\n",
      "4    中远juli\n",
      "5     远juli\n",
      "6     近juli\n",
      "Name: 里程, dtype: object\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## find 查找指定字符第一次出现的位置",
   "id": "44eadffd91cf4d7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T12:00:21.319971Z",
     "start_time": "2025-09-16T12:00:21.305001Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['日期'])\n",
    "print(df['日期'].astype(str).str.find('5'))\n",
    "print(df['日期'].astype(str).str.find('聂')) # 找不到返回-1\n",
    "print(df['日期'].astype(str).str.find('-',5)) # 从下标5开始往后找"
   ],
   "id": "c79382a95af33f48",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    2525-05-01 00:00:00\n",
      "1    2525-05-02 00:00:00\n",
      "2    2525-05-03 00:00:00\n",
      "3    2525-05-04 00:00:00\n",
      "4    2525-05-05 00:00:00\n",
      "5    2525-05-06 00:00:00\n",
      "6    2525-05-07 00:00:00\n",
      "Name: 日期, dtype: object\n",
      "0    1\n",
      "1    1\n",
      "2    1\n",
      "3    1\n",
      "4    1\n",
      "5    1\n",
      "6    1\n",
      "Name: 日期, dtype: int64\n",
      "0   -1\n",
      "1   -1\n",
      "2   -1\n",
      "3   -1\n",
      "4   -1\n",
      "5   -1\n",
      "6   -1\n",
      "Name: 日期, dtype: int64\n",
      "0    7\n",
      "1    7\n",
      "2    7\n",
      "3    7\n",
      "4    7\n",
      "5    7\n",
      "6    7\n",
      "Name: 日期, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 100
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 正则表达式\n",
   "id": "6243a8b33f58e1c5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T12:16:13.432985Z",
     "start_time": "2025-09-16T12:16:07.946898Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 邮箱正则表达式\n",
    "# wangzg@sina.com\n",
    "# 72789569.@qq.com\n",
    "# wangzg@jyd.cn.com\n",
    "import re\n",
    "email = input('请输入您的邮箱：')\n",
    "pattern = re.compile(r'[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(.com|.cn|.com.cn|.cn.com)')\n",
    "if re.search(pattern,email):\n",
    "    print(f'{email}邮箱格式正确')\n",
    "else:\n",
    "    print(f'{email}邮箱格式不正确')\n",
    "\n",
    "\n"
   ],
   "id": "c42968ed130c3ccd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wangsina.com邮箱格式不正确\n"
     ]
    }
   ],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T12:18:06.815563Z",
     "start_time": "2025-09-16T12:18:06.784053Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['状态'])\n",
    "print(df['状态'].str.match(\".{2}激\"))"
   ],
   "id": "81009031b3f12fb3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    满血激血活\n",
      "1     零血销毁\n",
      "2     满血激活\n",
      "3     满血激活\n",
      "4     零血销毁\n",
      "5     零血销毁\n",
      "6     零血销毁\n",
      "Name: 状态, dtype: object\n",
      "0     True\n",
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4    False\n",
      "5    False\n",
      "6    False\n",
      "Name: 状态, dtype: bool\n"
     ]
    }
   ],
   "execution_count": 106
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-16T12:25:37.826321Z",
     "start_time": "2025-09-16T12:25:37.800433Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '字符串.xlsx')\n",
    "print(df['日期'])\n",
    "# print(df[\"日期\"].astype('str').str.extract(r\"\\d{4}-(\\d{2})-(\\d{2})\"))\n",
    "print(df[\"日期\"].astype('str').str.replace(r\"(\\d+)-(\\d+)-(\\d+)\", r\"520\",regex=True))"
   ],
   "id": "5463c8f6632b697",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    2525-05-01 00:00:00\n",
      "1    2525-05-02 00:00:00\n",
      "2    2525-05-03 00:00:00\n",
      "3    2525-05-04 00:00:00\n",
      "4    2525-05-05 00:00:00\n",
      "5    2525-05-06 00:00:00\n",
      "6    2525-05-07 00:00:00\n",
      "Name: 日期, dtype: object\n",
      "0    520 00:00:00\n",
      "1    520 00:00:00\n",
      "2    520 00:00:00\n",
      "3    520 00:00:00\n",
      "4    520 00:00:00\n",
      "5    520 00:00:00\n",
      "6    520 00:00:00\n",
      "Name: 日期, dtype: object\n"
     ]
    }
   ],
   "execution_count": 112
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
